package com.lvmama.pangolin.util;

import com.lvmama.pangolin.entity.lmm.LMMTicketProduct;
import com.lvmama.pangolin.entity.mt.MTTicketProduct;
import com.lvmama.pangolin.entity.qnr.QNRTicketProduct;
import com.lvmama.pangolin.entity.tc.TCTicketProduct;
import com.lvmama.pangolin.entity.tn.TNTicketProduct;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketProduct;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 产品关联匹配处理类
 * <h1>产品关联维度</h1>
 * <h2>维度1：产品名称</h2>
 * <h2>维度2：产品省份、城市</h2>
 */
@Component
public class ProductRelationshipUtil {

    private static final Logger logger = Logger.getLogger(ProductRelationshipUtil.class);

    @Autowired
    private CommodityMatchUtil commodityMatchUtil;

    /**
     * 处理携程的产品匹配，根据城市，省份，名字和星级
     * @param lmmProduct
     * @param list
     */
    public void matchAndSortXCProduct(LMMTicketProduct lmmProduct, List<XCTicketProduct> list)
    {
        try{
            if(lmmProduct == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = lmmProduct.getSceneryName();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(lmmProduct.getSceneryName());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(XCTicketProduct item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getSceneryName());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float scoreName = commodityMatchUtil.score(segName,msegName);//名称评分
                        float scoreCityCode = commodityMatchUtil.score(lmmProduct.getCityCode(),item.getCityCode());
                        float scoreStartLevel = commodityMatchUtil.score(lmmProduct.getStarLevel(),item.getStarLevel());
                        float socreProvince = commodityMatchUtil.score(lmmProduct.getProvince(),item.getProvince());

                        float scorem = scoreName*0.4f + scoreCityCode*0.2f + scoreStartLevel*0.2f + socreProvince*0.2f;
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("携程商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<XCTicketProduct>()
            {
                public int compare(XCTicketProduct o1, XCTicketProduct o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<XCTicketProduct>() {
                @Override
                public int compare(XCTicketProduct o1, XCTicketProduct o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }

    }


    /**
     * 处理途牛的产品匹配，根据城市，省份，名字和星级
     * @param lmmProduct
     * @param list
     */
    public void matchAndSortTNProduct(LMMTicketProduct lmmProduct, List<TNTicketProduct> list)
    {
        try{
            if(lmmProduct == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = lmmProduct.getSceneryName();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(lmmProduct.getSceneryName());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(TNTicketProduct item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getSceneryName());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float scoreName = commodityMatchUtil.score(segName,msegName);//名称评分
                        float scoreCityCode = commodityMatchUtil.score(lmmProduct.getCityCode(),item.getCityCode());
                        float scoreStartLevel = commodityMatchUtil.score(lmmProduct.getStarLevel(),item.getStarLevel());
                        float socreProvince = commodityMatchUtil.score(lmmProduct.getProvince(),item.getProvince());

                        float scorem = scoreName*0.4f + scoreCityCode*0.2f + scoreStartLevel*0.2f + socreProvince*0.2f;
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("携程商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<TNTicketProduct>()
            {
                public int compare(TNTicketProduct o1, TNTicketProduct o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<TNTicketProduct>() {
                @Override
                public int compare(TNTicketProduct o1, TNTicketProduct o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }

    }


    /**
     * 处理同城的产品匹配，根据城市，省份，名字和星级
     * @param lmmProduct
     * @param list
     */
    public void matchAndSortTCProduct(LMMTicketProduct lmmProduct, List<TCTicketProduct> list)
    {
        try{
            if(lmmProduct == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = lmmProduct.getSceneryName();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(lmmProduct.getSceneryName());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(TCTicketProduct item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getSceneryName());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float scoreName = commodityMatchUtil.score(segName,msegName);//名称评分
                        float scoreCityCode = commodityMatchUtil.score(lmmProduct.getCityCode(),item.getCityCode());
                        float scoreStartLevel = commodityMatchUtil.score(lmmProduct.getStarLevel(),item.getStarLevel());
                        float socreProvince = commodityMatchUtil.score(lmmProduct.getProvince(),item.getProvince());

                        float scorem = scoreName*0.4f + scoreCityCode*0.2f + scoreStartLevel*0.2f + socreProvince*0.2f;
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("携程商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<TCTicketProduct>()
            {
                public int compare(TCTicketProduct o1, TCTicketProduct o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<TCTicketProduct>() {
                @Override
                public int compare(TCTicketProduct o1, TCTicketProduct o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }

    }




    /**
     * 处理美团的产品匹配，根据城市，省份，名字和星级
     * @param lmmProduct
     * @param list
     */
    public void matchAndSortMTProduct(LMMTicketProduct lmmProduct, List<MTTicketProduct> list)
    {
        try{
            if(lmmProduct == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = lmmProduct.getSceneryName();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(lmmProduct.getSceneryName());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(MTTicketProduct item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getSceneryName());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float scoreName = commodityMatchUtil.score(segName,msegName);//名称评分
                        float scoreCityCode = commodityMatchUtil.score(lmmProduct.getCityCode(),item.getCityCode());
                        float scoreStartLevel = commodityMatchUtil.score(lmmProduct.getStarLevel(),item.getStarLevel());
                        float socreProvince = commodityMatchUtil.score(lmmProduct.getProvince(),item.getProvince());

                        float scorem = scoreName*0.4f + scoreCityCode*0.2f + scoreStartLevel*0.2f + socreProvince*0.2f;
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("携程商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<MTTicketProduct>()
            {
                public int compare(MTTicketProduct o1, MTTicketProduct o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<MTTicketProduct>() {
                @Override
                public int compare(MTTicketProduct o1, MTTicketProduct o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }

    }



    /**
     * 处理去哪儿的产品匹配，根据城市，省份，名字和星级
     * @param lmmProduct
     * @param list
     */
    public void matchAndSortQNRProduct(LMMTicketProduct lmmProduct, List<QNRTicketProduct> list)
    {
        try{
            if(lmmProduct == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = lmmProduct.getSceneryName();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(lmmProduct.getSceneryName());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(QNRTicketProduct item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getSceneryName());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float scoreName = commodityMatchUtil.score(segName,msegName);//名称评分
                        float scoreCityCode = commodityMatchUtil.score(lmmProduct.getCityCode(),item.getCityCode());
                        float scoreStartLevel = commodityMatchUtil.score(lmmProduct.getStarLevel(),item.getStarLevel());
                        float socreProvince = commodityMatchUtil.score(lmmProduct.getProvince(),item.getProvince());

                        float scorem = scoreName*0.4f + scoreCityCode*0.2f + scoreStartLevel*0.2f + socreProvince*0.2f;
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("携程商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<QNRTicketProduct>()
            {
                public int compare(QNRTicketProduct o1, QNRTicketProduct o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<QNRTicketProduct>() {
                @Override
                public int compare(QNRTicketProduct o1, QNRTicketProduct o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }

    }





}
